"""
日志公共方法
"""
import logging
import os
from Common.handle_config import conf
from Common.handle_path import logs_dir


class MyLogger(logging.Logger):
    def __init__(self, name, level=logging.INFO, file=None):
        # 设置输出级别，输出渠道，输出日志格式
        super().__init__(name, level)

        # 设置渠道的输出内容格式
        fmt = '%(asctime)s %(name)s %(levelname)s %(filename)s-%(lineno)d line: %(message)s'
        formatter = logging.Formatter(fmt)

        # 控制台渠道
        handle1 = logging.StreamHandler()
        handle1.setFormatter(formatter)
        # 将设置好的渠道，添加到日志搜集器上
        self.addHandler(handle1)

        if file:
            # 文件渠道
            handle2 = logging.FileHandler(file, encoding='utf-8')
            handle2.setFormatter(formatter)
            # 将设置好的渠道，添加到日志搜集器上
            self.addHandler(handle2)


# 动态输入配置文件
if conf.getboolean("log", "file_ok"):
    file_name = os.path.join(logs_dir, conf.get("log", "file_name"))

else:
    file_name = None

name = conf.get("log", "name")
logger = MyLogger(name, file=file_name)

if __name__ == '__main__':
    file_name = os.path.join(logs_dir, conf.get("log", "file_name"))
    loger = MyLogger("自动化接口测试", file=file_name)
    loger.info("测试封装")
